package com.runner.unlocks;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.runner.observer.Event;
import com.runner.observer.Subject;
import com.runner.screen.ParameterScreen;
import com.runner.utility.Constants;
import com.runner.utility.Locator;

public class Track2Unlock implements Constants {

	public static final int TRACK2_AMOUNT = 100;
	private boolean unlocked;
	private ImageButton button;
	private Subject subject;
	private Label unlockField, name;
	private Stage stage;
	private Image coinImage;
	private ParameterScreen param;

	public Track2Unlock(ParameterScreen param, Stage stage, LabelStyle style, Subject subject) {
		this.subject = subject;
		this.stage = stage;
		this.param=param;
		unlocked = Locator.getUnlocks().isRingerTrack2();
		createButton(style);
	}

	public void createButton(LabelStyle style) {
		Image i = new Image(Locator.getAssets().get(TRACK2_PICTURE, Texture.class));
		button = new ImageButton(i.getDrawable());
		button.addListener(new ClickListener() {
			@Override
			public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
				subject.notify(Event.EVENT_TRACK2_SELECTED);
				return false;
			}
		});
		button.setPosition(ACTUAL_VIRTUAL_WIDTH * (TRACK_X + SEPARATION_X), ACTUAL_VIRTUAL_HEIGHT * (TRACK_Y+ HEIGHT_ADJUST));
		button.getImage().setScale(1 );
		button.setScale(1 );
		stage.addActor(button);

		unlockField = new Label("Unlock "+TRACK2_AMOUNT, style);
		unlockField.setPosition(ACTUAL_VIRTUAL_WIDTH * (TRACK_X + SEPARATION_X + 0.01f), ACTUAL_VIRTUAL_HEIGHT * (TRACK_Y + 0.01f+ HEIGHT_ADJUST));

		name = new Label("Ringer Track 2", style);
		name.setPosition(ACTUAL_VIRTUAL_WIDTH * (TRACK_X + SEPARATION_X + 0.009f), ACTUAL_VIRTUAL_HEIGHT * (TRACK_Y + 0.16f+ HEIGHT_ADJUST));
		stage.addActor(name);

		coinImage = new Image(Locator.getAssets().get(SINGLE_COIN_PATH, Texture.class));
		coinImage.setPosition(ACTUAL_VIRTUAL_WIDTH * (TRACK_X + SEPARATION_X + 0.10f), ACTUAL_VIRTUAL_HEIGHT * (TRACK_Y + 0.015f+ HEIGHT_ADJUST));
		coinImage.setScale((1 ) * 0.57F);

		stage.addActor(unlockField);
		stage.addActor(coinImage);
		if (unlocked) {
			unlockField.setVisible(false);
			coinImage.setVisible(false);
		}
	}
	
	/**
	 * displays if track 2 has been unlocked
	 * @param pType
	 */
	public void switchButton(PLAYER_TYPE pType) {
		switch (pType) {
		case RINGER:
			name.setText("Ringer Track 2");
			if(!Locator.getUnlocks().isRingerTrack2()){
				unlockField.setVisible(true);
				coinImage.setVisible(true);
				unlocked=false;
				subject.notify(Event.EVENT_TRACK1_SELECTED);
			} else {
				unlockField.setVisible(false);
				coinImage.setVisible(false);
			}
			break;
		case APRIL:
			name.setText("April Track 2");
			if(!Locator.getUnlocks().isAprilTrack2()){
				unlockField.setVisible(true);
				coinImage.setVisible(true);
				unlocked=false;
				subject.notify(Event.EVENT_TRACK1_SELECTED);
			} else {
				unlockField.setVisible(false);
				coinImage.setVisible(false);
			}
			break;
		case GEO:
			name.setText("Geo Track 2");
			if(!Locator.getUnlocks().isGeoTrack2()){
				unlockField.setVisible(true);
				coinImage.setVisible(true);
				unlocked=false;
				subject.notify(Event.EVENT_TRACK1_SELECTED);
			} else {
				unlockField.setVisible(false);
				coinImage.setVisible(false);
			}
			break;
		}
	}

	public boolean unlock() {
		PLAYER_TYPE pType=param.getPType();
		if (Locator.getCoins() >= TRACK2_AMOUNT) {
			switch (pType) {
			case RINGER:
				Locator.getUnlocks().setRingerTrack2(true);
				break;
			case APRIL:
				Locator.getUnlocks().setAprilTrack2(true);
				break;
			case GEO:
				Locator.getUnlocks().setGeoTrack2(true);
				break;
			}
			unlocked = true;
			Locator.removeCoins(TRACK2_AMOUNT);
			unlockField.setVisible(false);
			coinImage.setVisible(false);
			Locator.writeSave();
			subject.notify(Event.EVENT_TRACK2_UNLOCKED);
			subject.notify(Event.EVENT_TRACK2_SELECTED);
			return true;
		} else {
			return false;
		}
	}

}
